package com.gitee.sop.layui.adminserver.module.background.admin.rest;

import com.gitee.sop.layui.adminserver.module.background.admin.dto.SysMenuDTO;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenu;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysMenuService;
import com.gitee.sop.layui.adminserver.module.base.bo.ValidGroup;
import com.gitee.sop.layui.adminserver.module.base.bo.ZTreeNode;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.error.ServiceInterruptionException;
import com.gitee.sop.layui.adminserver.module.base.util.AssertUtil;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import com.gitee.sop.layui.adminserver.module.base.vo.MenuTreeVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * com.zch.admin.base.entity.SysMenu模型http接口处理
 *
 * @author: admin
 * @date: 2019-11-14
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/admin/menu")
public class SysMenuRestController {

    @Lazy
    @Resource
    private SysMenuService baseService;

    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(@Validated({ValidGroup.C1.class}) SysMenuDTO query) {
        SysMenu sysMenu = new SysMenu();
        ObjectUtil.switchAllFieldValue(sysMenu, query);
        baseService.add(query.getPid(), sysMenu);
        return Result.success();
    }

    @PostMapping(value = {"/append"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result append(int menuId, String name, String uri, String description, String icon) {
        AssertUtil.notEmpty(name, ServiceInterruptionException.fail(400, "菜单名称不能为空"));
        AssertUtil.notEmpty(uri, ServiceInterruptionException.fail(400, "uri地址不能为空"));

        baseService.append(menuId, name, uri, description, icon);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "id") int id) {
        baseService.deleteSelfAndChildrenById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "id") int id, SysMenuDTO query) {
        SysMenu sysMenu = new SysMenu();
        ObjectUtil.switchAllFieldValue(sysMenu, query);
        baseService.updateById(sysMenu.setId(id));
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchAll(String appId) {
        List<Map<String, Object>> list = baseService.getListAndOrderPidSortByAppId(appId);
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") int id) {
        SysMenu SysMenu = baseService.getById(id);
        return Result.success(SysMenu);
    }

    /**
     * 后台首页菜单导航树
     *
     * @return
     */
    @RequestMapping(value = {"/search/vertical/tree"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchVerticalTree() {
        List<MenuTreeVo> list = baseService.searchVerticalMenuTree();
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/level/tree/{id}"})
    public Result searchLevelTree(@PathVariable("id") int id) {
        List<MenuTreeVo> list = baseService.searchLevelMenuTree(id);
        return Result.success(list);
    }

    /**
     * 用户角色分配树
     *
     * @param roleUuid
     * @return
     */
    @RequestMapping(value = {"/search/tree/{roleUuid}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchMenuTreeByRoleId(@PathVariable(value = "roleUuid") String roleUuid, String appId) {
        List<ZTreeNode> list = baseService.searchMenuTreeByRoleUuid(appId, roleUuid);
        return ObjectUtil.toJson(list);
    }

    @RequestMapping(value = {"/search/use/template"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchMenuTreeForUseTemplate() {
        List<ZTreeNode> list = baseService.getMenuTreeForUseTemplate();
        return ObjectUtil.toJson(list);
    }

    @RequestMapping(value = {"/set/template/menu/{appId}"})
    public Result setMenuByUseMenuTemplate(@PathVariable(value = "appId") String appId, @RequestParam(value = "menuIds") Integer[] menuIds) {
        AssertUtil.isTrue(menuIds.length < 1000, ServiceInterruptionException.fail(400, "参数错误"));
        baseService.setMenuByUseMenuTemplate(appId, Arrays.stream(menuIds).collect(Collectors.toList()));
        return Result.success();
    }
}
